#pragma once

#include <iostream>
#include <memory>
#include <dirent.h>
#include <sys/stat.h>
#include "segmentation_api.h"
#include <algorithm>
#include <chrono>
using namespace segmentation;

int64_t CurrentTimeMillis()
{
        int64_t timems = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
        return timems;
}
void FindFiles(const std::string &path, std::vector<std::string> &files)
{
        struct stat rst;
        if (stat(const_cast<char *>(path.c_str()), &rst) != 0)
                return;
        DIR *dirpt = opendir(path.data());
        dirent *entry;
        while (entry = readdir(dirpt))
        {
                /* code */
                if (entry->d_type == DT_REG)
                        files.emplace_back(entry->d_name);
        }
        closedir(dirpt);
}

void test_segmentation()
{
        std::shared_ptr<Segmentation_api> segment = std::make_shared<Segmentation_api>();

        std::cout << "test" << std::endl;
        // cv::Mat img = cv::Mat::zeros(270, 480, CV_8UC3);

        // std::string root="/home/klyr/4T/seg/data/testresult/imgs1/";
        std::string root = "/home/user/yyz_workspace_disk/Code/algolib/img/";
        std::vector<std::string> files;
        FindFiles(root, files);
        cv::Mat output3;
        for (int i = 0; i < files.size(); i++)
        {
                std::cout << files[i] << std::endl;
                std::string file_path = root + files[i]; // "/home/administrator/imgs/[4]14_21_33.jpg";   // ## '/home/xixi/1.jpg'
                cv::Mat img = cv::imread(file_path);
                if (img.empty())
                {
                        std::cout << "img is empty()" << std::endl;
                        continue;
                }
                cv::Mat img_t = img.clone();

                // algocommon::MatPad mp(img_t.cols, img_t.rows);
                int64_t start_time = CurrentTimeMillis();

                cv::Mat output = segment->Execute(img);
                // std::cout << "---wwwwwwwwwww--------------->time is----------->: " << CurrentTimeMillis() - start_time << " ms" << std::endl;
                // cv::imwrite("../output_images/output.jpg", output);
                cv::imwrite("../output_images/mask_" + files[i], output);

                // cv::imshow("dddddd", output);
                // cv::imshow("dddddd1", img);

                // cv::waitKey();
                // std::cout << "------------------>time is----------->: " << CurrentTimeMillis() - start_time << " ms" << std::endl;
                std::cout << "###------------------>time is----------->: " << CurrentTimeMillis() - start_time << " ms" << std::endl;

                // std::cout << "cv::Mat output:::" << output.size() << std::endl;
                // std::cout << "cv::Mat output c:::" << output.channels() << std::endl;
                // img = mp(img);
                cv::resize(output, output, img.size(), cv::INTER_LINEAR);
                // cv::resize(img, img, output.size(), cv::INTER_LINEAR);

                // std::cout << "cv::Mat output resize:::" << output.size() << std::endl;
                for (int row = 0; row < output.rows; row++)
                {
                        for (int col = 0; col < output.cols; col++)
                        {
                                if (output.at<uchar>(row, col) == 13) // road
                                {
                                        // output.at<uchar>(i, j) = 255;
                                        img.at<cv::Vec3b>(row, col)[0] = 0.5 * img.at<cv::Vec3b>(row, col)[0] + 0.5 * 0;
                                        img.at<cv::Vec3b>(row, col)[1] = 0.5 * img.at<cv::Vec3b>(row, col)[1] + 0.5 * 255;
                                        img.at<cv::Vec3b>(row, col)[2] = 0.5 * img.at<cv::Vec3b>(row, col)[2] + 0.5 * 0;
                                }
                                else if (output.at<uchar>(row, col) == 12) // sideway
                                {
                                        // output.at<uchar>(i, j) =   100;
                                        img.at<cv::Vec3b>(row, col)[0] = 0.5 * img.at<cv::Vec3b>(row, col)[0] + 0.5 * 255;
                                        img.at<cv::Vec3b>(row, col)[1] = 0.5 * img.at<cv::Vec3b>(row, col)[1] + 0.5 * 0;
                                        img.at<cv::Vec3b>(row, col)[2] = 0.5 * img.at<cv::Vec3b>(row, col)[2] + 0.5 * 0;
                                }
                                else if (output.at<uchar>(row, col) == 14) // building
                                {
                                        img.at<cv::Vec3b>(row, col)[0] = 0.5 * img.at<cv::Vec3b>(row, col)[0] + 0.5 * 0;
                                        img.at<cv::Vec3b>(row, col)[1] = 0.5 * img.at<cv::Vec3b>(row, col)[1] + 0.5 * 0;
                                        img.at<cv::Vec3b>(row, col)[2] = 0.5 * img.at<cv::Vec3b>(row, col)[2] + 0.5 * 255;
                                }
                                else
                                {
                                        img.at<cv::Vec3b>(row, col)[0] = 0.5 * img.at<cv::Vec3b>(row, col)[0] + 0.5 * 255;
                                        img.at<cv::Vec3b>(row, col)[1] = 0.5 * img.at<cv::Vec3b>(row, col)[1] + 0.5 * 255;
                                        img.at<cv::Vec3b>(row, col)[2] = 0.5 * img.at<cv::Vec3b>(row, col)[2] + 0.5 * 255;
                                }
                        }
                }
                cv::imshow(files[i], img);
                cv::imwrite("../output_images/" + files[i], img);
                // cv::imshow("img_t", img_t);
                // cv::imwrite("/home/klyr/4T/seg/data/testresult/result_rt/image_rt/"+files[i], img_t);

                char key = cv::waitKey();
                if (key == 'q')
                        break;
                cv::destroyAllWindows();
        }
}
